package com.xiaoma.myaudience.biz.model;

import com.xiaoma.myaudience.biz.util.VideoStringUtils;
import com.xiaoma.myaudience.util.model.BaseInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LatestCommentInfo extends BaseInfo {
	
    /**返回值标示码*/
    public static final String PARAM_CODE = "code";
    /**原文URL*/
    public static final String PARAM_DOCURL = "docUrl";
    /**跟贴参与数字*/
    public static final String PARAM_PRCOUNT = "prcount";
    /**跟贴数字*/
    public static final String PARAM_PTCOUNT = "ptcount";
    /**返回是否有隐藏楼层*/
    public static final String PARAM_NON = "NON";
    /**热门跟贴列表*/
    public static final String PARAM_LATEST_LIST = "newPosts";
    
    /**==========================一栋楼参数==========================================*/
    /**P标签*/
    public static final String PARAM_P = "p";
    /**跟贴内容*/
    public static final String PARAM_B = "b";
    /**跟贴用户标示*/
    public static final String PARAM_F = "f"; 
    /**用户账号*/
    public static final String PARAM_U = "u";
    /**楼层用户顶数*/
    public static final String PARAM_V = "v";
    /**发帖时间*/
    public static final String PARAM_T = "t";
    /**跟贴IP*/
    public static final String PARAM_IP = "ip";
    /**跟贴L tag*/
    public static final String PARAM_L = "l";
    /**跟贴版块ID*/
    public static final String PARAM_BOARDID_BI = "bi";
    
    public String mCode;
    public String mDocUrl;
    public String mPrcount;
    public String mPtcount;
    public List<ArrayList<HashMap<String, Object>>> mLatesttList;
    public Map<String, Object> mPandNONValue;
    
    /***
     * 从JSONObject中读取到Info中
     */
    @Override
    public void readFromJSONObject(JSONObject json) throws JSONException{
        super.readFromJSONObject(json);
        
        try {
	        if (json != null) {
	        	
	        	String code = json.getString(PARAM_CODE);
	        	mCode = code;
	        	mDocUrl = json.getString(PARAM_DOCURL);
	        	
	        	if(null != code && "1".equals(code) && !json.isNull(PARAM_LATEST_LIST)){ // 正确数据
	        		mPrcount = json.getString(PARAM_PRCOUNT);
		        	mPtcount = json.getString(PARAM_PTCOUNT);
		        	
	        		JSONArray jsonArray = json.getJSONArray(PARAM_LATEST_LIST); // 获取跟贴列表
	        		
	            	int len = jsonArray.length();
	            	
	            	List<ArrayList<HashMap<String, Object>>> buildingList = new ArrayList<ArrayList<HashMap<String, Object>>>();
	            	for(int i = 0; i < len; i ++){ // 多少栋楼
	            		JSONObject oneBuilding = jsonArray.getJSONObject(i);
	            		ArrayList<HashMap<String, Object>> floorList = new ArrayList<HashMap<String, Object>>();
	            		if(null != oneBuilding){
	            			int floorNums = oneBuilding.length();
	            			if(!oneBuilding.isNull(PARAM_NON)){ // 有隐藏楼层时，楼层个数为 NON 
	            				floorNums = Integer.parseInt(oneBuilding.getString(PARAM_NON));
	            			}
	            			for(int j = floorNums - 1; j >= 0; j --){ // 读取每一栋楼的数据
	            				
	            				if(oneBuilding.has(String.valueOf(j + 1))){ // 有最后一层
	            					
	            					JSONObject oneFloor = oneBuilding.getJSONObject(String.valueOf(j + 1));
	            					
	            					HashMap<String, Object> oneFloorMap = new HashMap<String, Object>();
	            							
            						if(!oneFloor.isNull(PARAM_P)) {
            							oneFloorMap.put(PARAM_P, oneFloor.getString(PARAM_P));
										if(j==len-1 && !oneBuilding.isNull(PARAM_NON)) 
											mPandNONValue.put(oneFloor.getString(PARAM_P), oneBuilding.getString(PARAM_NON));
									}else oneFloorMap.put(PARAM_P, "");
            						
									if(!oneFloor.isNull(PARAM_V))
										oneFloorMap.put(PARAM_V, oneFloor.getString(PARAM_V));
									else oneFloorMap.put(PARAM_V, "");
									
									if(!oneFloor.isNull(PARAM_U))
										oneFloorMap.put(PARAM_U, VideoStringUtils.htmlDecoder(oneFloor.getString(PARAM_U)));
									else oneFloorMap.put(PARAM_U, "");
									
									if(!oneFloor.isNull(PARAM_T))
										oneFloorMap.put(PARAM_T, oneFloor.getString(PARAM_T));
									else oneFloorMap.put(PARAM_T, ""); 
									
									if(!oneFloor.isNull(PARAM_B))
										oneFloorMap.put(PARAM_B, VideoStringUtils.htmlDecoder(oneFloor.getString(PARAM_B)));
									else oneFloorMap.put(PARAM_B, "");
									
									if(!oneFloor.isNull(PARAM_IP))
										oneFloorMap.put(PARAM_IP, oneFloor.getString(PARAM_IP));
									else oneFloorMap.put(PARAM_IP, "");
									
									if(!oneFloor.isNull(PARAM_F))
										oneFloorMap.put(PARAM_F, VideoStringUtils.getUserName(VideoStringUtils.htmlDecoder(oneFloor.getString(PARAM_F))));
									else oneFloorMap.put(PARAM_F, "");
									
									if(!oneFloor.isNull(PARAM_L))
										oneFloorMap.put(PARAM_L, oneFloor.getString(PARAM_L));
									else oneFloorMap.put(PARAM_L, "");
									
									if(!oneFloor.isNull(PARAM_BOARDID_BI))
										oneFloorMap.put(PARAM_BOARDID_BI, oneFloor.getString(PARAM_BOARDID_BI));
									else oneFloorMap.put(PARAM_BOARDID_BI, "");
            						
									floorList.add(oneFloorMap);
								
	            				} 
	            				
	            			} // 循环一栋楼结束, 将这栋楼加到热门跟贴的数据中
		            		buildingList.add(floorList);
	            		}
	            	} // 所有数据读取完毕，将数据赋值给热门跟贴的list
	            	mLatesttList = buildingList;
	        	}
	        	
	        }
        } catch (Exception e) {
			e.printStackTrace();
		}
		
    }
    
    @Override
    public void writeToParcel(Parcel parcel, int paramInt) {
        super.writeToParcel(parcel, paramInt);
       
        parcel.writeString(mCode);
        parcel.writeString(mDocUrl);
        parcel.writeList(mLatesttList);
        parcel.writeMap(mPandNONValue);
        
    }  
    
    @SuppressWarnings("unchecked")
	@Override
    public void readFromParcel(Parcel source) {
        super.readFromParcel(source);
        
        mCode = source.readString();
        mDocUrl = source.readString();
        
        ClassLoader loader = java.util.ArrayList.class.getClassLoader();
        mLatesttList =  source.readArrayList(loader);
        
        loader = java.util.HashMap.class.getClassLoader();
        mPandNONValue = source.readHashMap(loader);
    }

    public static final Parcelable.Creator<LatestCommentInfo> CREATOR = new Creator<LatestCommentInfo>() {
        public LatestCommentInfo createFromParcel(Parcel source) {
        	LatestCommentInfo info = new LatestCommentInfo();
            info.readFromParcel(source);
            return info;
        }

        public LatestCommentInfo[] newArray(int size) {
            return new LatestCommentInfo[size];
        }
    };
    
}
